package com.hmjk.health.home;


import com.hmjk.health.HealthApp;
import com.hmjk.health.R;
import com.hmjk.health.background.DataManager;
import com.hmjk.health.util.DEV_DATA;
import com.hmjk.health.util.DEV_SET;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

public class DeviceDetailActivity extends Activity implements OnClickListener{
	private HealthApp mApp = null;	
	
	private DEV_SET mSetData;
	private DEV_DATA mDev = null;
	
	//for health dev
	//private ArrayList<MULTI_USER_DEV_DATA> mHealthAccountList = new ArrayList<MULTI_USER_DEV_DATA>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mApp = (HealthApp)getApplication();
		Intent intent = getIntent();
		
		mDev = (DEV_DATA)intent.getSerializableExtra("dev");
		
		mSetData = null;
		
		String[] args = new String[1];
		args[0] = mDev.id;
		
		DEV_SET[] data = (DEV_SET[]) mApp.getDataManager().checkData(DataManager.F_DEVICE_SETTINGS, args);
		if(data != null){
			mSetData =  data[0];
		}
		
		setContentView(R.layout.devicedetail_activity);
		
		ImageButton back = (ImageButton) findViewById(R.id.back);
		back.setOnClickListener(this);
		
		View loca_view = findViewById(R.id.loca_view);
		View health_view = findViewById(R.id.health_view);
		TextView healthTxt = (TextView) findViewById(R.id.health_info);
		View sport_view = findViewById(R.id.sport_view);
		
		if(mDev.type.contains(DEV_DATA.TYPE_LOCATE)){
			loca_view.setVisibility(View.VISIBLE);
		}else{
			loca_view.setVisibility(View.GONE);
		}
		String healthstring = "";
		if(mDev.type.contains(DEV_DATA.TYPE_HEART_PULSE)){
			healthstring += getResources().getString(R.string.dev_type_puls) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_GLUCOSE_METER)){
			healthstring += getResources().getString(R.string.dev_type_gluc) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_BLOOD_PRESSURE_METER)){
			healthstring += getResources().getString(R.string.dev_type_pres) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_SLEEP_MONITOR)){
			healthstring += getResources().getString(R.string.dev_type_slep) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_THERMOMETER)){
			healthstring += getResources().getString(R.string.dev_type_temp) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_OXIMETER)){
			healthstring += getResources().getString(R.string.dev_type_spo2) + " ";
		}
		if(mDev.type.contains(DEV_DATA.TYPE_ELECTGRAPH)){
			healthstring += getResources().getString(R.string.dev_type_hecg) + " ";
		}
		
		if(healthstring.trim().length() > 0){
			health_view.setVisibility(View.VISIBLE);
			healthTxt.setText(healthstring.trim());
		}else{
			health_view.setVisibility(View.GONE);
		}
		
		if(mDev.type.contains(DEV_DATA.TYPE_STEP_COUNTER)){
			sport_view.setVisibility(View.VISIBLE);
		}else{
			sport_view.setVisibility(View.GONE);
		}
		
		TextView modelview = (TextView) findViewById(R.id.devmodel);
		String model = "";
		if(mSetData != null){
			model = mSetData.model;
		}
		modelview.setText(model);
		
		TextView manuview = (TextView) findViewById(R.id.devmanufac);
		String manufac = "";
		if(mSetData != null){
			manufac = mSetData.manufacture;
		}
		manuview.setText(manufac);
		
		TextView issueview = (TextView) findViewById(R.id.devissued);
		String issue = "";
		if(mSetData != null){
			issue = mSetData.issued;
		}
		issueview.setText(issue);
		
		TextView snview = (TextView) findViewById(R.id.devSN);
		String sn = "";
		if(mSetData != null){
			sn = mSetData.sn;
		}
		snview.setText(sn);
		
		TextView mCheckDid = (TextView)findViewById(R.id.checkdid);
		if((mSetData != null) && (mDev.admin == 1)){
			String toDeviceID = getResources().getString(R.string.dev_type_check_did);
			mCheckDid.setText(Html.fromHtml(toDeviceID));
			mCheckDid.setOnClickListener(this);
		}else {
			mCheckDid.setVisibility(View.GONE);
		}
		
	}
	
	/*private void getHealthAccounts(){
		String[] args = new String[2];
		args[0] = "TID";
		args[1] = mTid;
		JSONObject subinfo = (JSONObject) mApp.getDataManager().checkData(DataManager.F_DEVICESUBINFO,args);
		mHealthAccountList.clear();
		
		if(subinfo != null){
			try {
			JSONArray subar = subinfo.getJSONArray("array");

			int sublen = subar.length();
			if(sublen > 0){
				MULTI_USER_DEV_DATA[] temp = new MULTI_USER_DEV_DATA[sublen];
				for(int j = 0; j < sublen; j++){
					JSONObject subone = subar.getJSONObject(j);
					MULTI_USER_DEV_DATA d = new MULTI_USER_DEV_DATA();
					getString("tid");
					d.name = mDevName;
					d.type = mDevType;
					
					
					String scode = subone.getString("subcode");
					d.subcode = scode;
					d.nick = subone.getString("nickname");
					d.birthday = subone.getString("birthday");
					d.gender = subone.getString("gender");
					d.height = subone.getDouble("height");
					d.weight = subone.getDouble("weight");
					temp[j] = d;
				}
				Arrays.sort(temp,0,sublen,new Comparator<MULTI_USER_DEV_DATA>(){
					@Override
					public int compare(MULTI_USER_DEV_DATA arg0, MULTI_USER_DEV_DATA arg1) {
						int flags = 0;
						int t0 = Integer.parseInt(arg0.subcode);
						int t1 = Integer.parseInt(arg1.subcode);
						if (t0 < t1) {
							flags = -1;
						}else if(t0 > t1) {
							flags = 1;
						}else{
							flags = 0;
						}
						return flags;
					}
					
				});
				for(MULTI_USER_DEV_DATA d:temp){
					mHealthAccountList.add(d);
				}
			}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/
	
	
	@Override
	public void onResume(){
		super.onResume();
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.back:{
			finish();
		}break;
		
		case R.id.checkdid:{
			Intent i = new Intent(this, DeviceIDActivity.class);
			i.putExtra("mobile", mSetData.mobile);
			i.putExtra("did", mSetData.did);
			startActivityForResult(i, RESULT_CODE_DID);
		}break;
		}
		
	}
	
	private int RESULT_CODE_DID = 555;
	
	@Override
	public void onActivityResult(int requestCode, int result, Intent intent){
		if(RESULT_CODE_DID == requestCode){
			if(RESULT_OK == result){
				
				Intent i = new Intent(this,DeviceManagerEdit.class);
				i.putExtra("dev", mDev);
				startActivity(i);
				//return because send did whithout phone number
				finish();
			}
		}
	}
}
